<template>
    <el-form :model="formItem">
        <el-form-item label="账号">
            <el-input v-model="formItem.account" />
        </el-form-item>
        <el-form-item label="密码">
            <el-input type="password" v-model="formItem.password" />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onLogin">登录</el-button>
        </el-form-item>
    </el-form>
</template>

<script setup lang="ts">
import { adminLoginApi } from '@/api/admin';
import { ref } from 'vue';
import { useRouter } from 'vue-router';
const router = useRouter();

const formItem = ref<any>({
    account: 'lisi',
    password: '123'
});

const onLogin = async () => {
    const res = await adminLoginApi(formItem.value);
    if (res.code == 200) {
        // 如果希望在有效期内，不用重复登录，将token保存到localStorage
        // 如果希望在有效期内，浏览器关闭后，需要重新登录，将token保存到sessionStorage
        localStorage.admin_token = res.token;
        ElMessage.success('登录成功！');
        // router.replace() 跳转后不可回退
        router.replace('/admin/index');
    } else {
        ElMessage.error(res.message);
    }
}
</script>

<style scoped></style>